﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;


namespace HPiLO2
{
    public partial class SelectClients : Form
    {
        public SelectClients()
        {
            InitializeComponent();
        }
        List<iLOClient> _iLOClients = new List<iLOClient> { };
        #region Save & Load
        private void LoadFiles()
        {
            try
            {
                using (Stream stream = File.Open("data.bin", FileMode.Open))
                {
                    BinaryFormatter bin = new BinaryFormatter();

                    var TempClients = (List<iLOClient>)bin.Deserialize(stream);
                    if (TempClients.Count > 0)
                    {
                        _iLOClients = TempClients;
                    }
                }
            }
            catch (IOException)
            {
            }
        }
     

        private void SaveFiles()
        {
            try
            {
                using (Stream stream = File.Open("data.bin", FileMode.Create))
                {
                    BinaryFormatter bin = new BinaryFormatter();
                    bin.Serialize(stream, _iLOClients);
                }
            }
            catch { }

        }
        #endregion
        public void _PrepAndOpen()
        {
            LoadFiles();
            UpdateList();
            this.Resize+=new EventHandler(SelectClients_Resize);
            this.Show();
            this.Activate();
        }
        private void UpdateList()
        {
            _ClientList.Items.Clear();
            foreach (iLOClient Client in _iLOClients)
            {
                _ClientList.Items.Add(Client.FriendlyName, Client.Enabled);

            }
        }
        private void SelectClients_Resize(object sender, EventArgs e)
        {

        }

        #region Buttons
        private void _BtnDone_Click(object sender, EventArgs e)
        {
            SaveFiles();
            this.Hide();

        }

        private void _btnAddNew_Click(object sender, EventArgs e)
        {
            SetBtnStates(false);

        }
        /// <summary>
        /// Set draw states, true = normal, false = adding new / modifying 
        /// </summary>
        /// <param name="bottombar"></param>
        private void SetBtnStates(bool bottombar)
        {
            _GBnewiLO.Visible = !bottombar;
            _GBnewiLO.Enabled = !bottombar;
            _btnAddNew.Enabled = bottombar;
            _btnDeleteClient.Enabled = bottombar;
            _BtnDone.Enabled = bottombar;
            _btnEditClient.Enabled = bottombar;
            _btnOkay.Enabled = !bottombar;
            _btnCancel.Enabled = !bottombar;
        }

        #endregion

        private void _btnOkay_Click(object sender, EventArgs e)
        {
            //TODO make it update existing automatically
            iLOClient newclient = new iLOClient(_tbURL.Text.Trim(), _tbpassword.Text.Trim(), _tbFriendlyName.Text.Trim(), _tbUsername.Text.Trim(), _cbEnabled.Text == "True" ? true : false,_ConnectionSSH.Text =="SSH"?true:false);
            _iLOClients.Add(newclient);
            newclient = null;
            SaveFiles();
            ClearTxtBoxes();
            SetBtnStates(true);
            UpdateList();
        }
        private void ClearTxtBoxes()
        {
            _tbFriendlyName.Text = "";
            _tbpassword.Text = "";
            _tbURL.Text = "";
            _tbUsername.Text = "";
        }
        private void _btnCancel_Click(object sender, EventArgs e)
        {
            ClearTxtBoxes();
            SetBtnStates(true);
            UpdateList();
        }

        private void _btnDeleteClient_Click(object sender, EventArgs e)
        {
            if (_ClientList.SelectedIndex >= 0)
            {
                _iLOClients.RemoveAt(_ClientList.SelectedIndex);
                _ClientList.Items.RemoveAt(_ClientList.SelectedIndex);
                UpdateList();
            }
        }


        
    }
}
